<?php
namespace app\shop\controller;
use think\Db;
class Product extends Base{
    public function product(){
        $keyword=input('get.keyword');
        $c_id=input('get.c_id');
        $sort_type=input('get.sort_type');
        $discount=input('get.discount');
        $map = [
            ['a.state', '=', 1]
        ];
        if($c_id){
            array_push($map,['a.c_id','=',$c_id]);
        }
        if($discount==1){
            array_push($map,['a.discount','=',$discount]);
        }
        if($sort_type==1){
            $orderby='a.sales desc';
        }elseif($sort_type==2){
            $orderby='a.sales asc';
        }else{
            $orderby = 'a.sort desc';
        }
    	$cates=Db::name('cates')->where('f_id',0)->where('state',1)->select();

        $products=Db::name('product')->alias('a')->join('cates b', 'a.c_id = b.id','LEFT')->field('a.*,b.catename')->where($map)->where('a.title','like',"%{$keyword}%")->order($orderby)->paginate(12,false,['query' => request()->param()]);
        $items = $products->items();
        if($items){
            foreach ($items as $key => $value) {
                $items[$key]['price']=Db::name('spec_combination')->where('p_id',$items[$key]['id'])->where('state',1)->order('id asc')->value('money');
            }
        }
        
        $this->assign('cates',$cates);
        $this->assign('keyword',$keyword);
        $this->assign('discount',$discount);
        $this->assign('c_id',$c_id);
        $this->assign('sort_type',$sort_type);
        $this->assign('products',$products);
        $this->assign('items',$items);
        $this->assign('url',$this->request->domain());
        $this->assign('system',$this->system_set());
        return $this->fetch();
    }
    public function product_detail(){
        $id=input("param.id");
        $url=$this->request->domain();
        $cates=Db::name('cates')->where('f_id',0)->where('state',1)->select();
        $productDetail=Db::name('product')->where('id',$id)->find();
        $productNew=Db::name('product')->where('state',1)->order('id desc')->limit(2)->select();
        if($productDetail){
            
            $productDetail['imgs']=explode(',',$productDetail['imgs']);
            $productDetail['price']=Db::name('spec_combination')->where('p_id',$id)->where('state',1)->order('id asc')->value('money');
            $productDetail['spec']=Db::name('spec')->where('p_id',$id)->where('state',1)->order('id asc')->select();
            if ($productDetail['spec']) {
                foreach ($productDetail['spec'] as $key => $value) {
                    $productDetail['spec'][$key]['spec_vals']=Db::name('spec_val')->where('p_id',$id)->where('spec_id',$productDetail['spec'][$key]['id'])->where('state',1)->order('id asc')->select();
                }
            }
            $productDetail['spec_combination']=Db::name('spec_combination')->where('p_id',$id)->where('state',1)->order('id asc')->select();
        }
        $this->assign('productDetail',$productDetail);
        $this->assign('cates',$cates);
        $this->assign('url',$url);
        $this->assign('id',$id);
        $this->assign('productNew',$productNew);
        $this->assign('system',$this->system_set());
        return $this->fetch();
    }
    public function evaluates()
    {
        if(!empty(input("param.p_id"))){
            $p_id=input("param.p_id");
        }else{
            return json(['code' => 501, 'msg' => '产品或门票id']);
        }
        if(!empty(input("param.page"))){
            $page=input("param.page");
        }else{
            return json(['code' => 502, 'msg' => '当前分页数']);
        }
        if(input("param.type")==1||input("param.type")==2){
            $data['type']=input("param.type");
        }else{
            return json(['code' => 503, 'msg' => '产品还是门票']);
        }
        $res=Db::name('evaluates')->alias('a')->join('users b', 'a.user_id = b.id','LEFT')->field('a.*,b.headimgurl,b.nickname,b.phone')->where('a.p_id',$p_id)->where('a.state',1)->where('a.type',$data['type'])->order('a.id desc')->limit(10*($page-1),10)->select();
        if ($res) {
            foreach ($res as $key => $value) {
                $res[$key]['phone'] = substr_replace($res[$key]['phone'], '****', 3, 4);
                $res[$key]['time'] = date('Y-m-d H:i:s',$res[$key]['time']);
            }
            return json(['code' => 200, 'msg' => '成功！','data'=>$res]);
        }else{
            return json(['code' => 500, 'msg' => '查无数据！']);
        }
    }
}
